﻿using Microsoft.AspNetCore.Mvc;
using TaskPollingWebApi.Services;

namespace TaskPollingWebApi.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class TaskPollingController : ControllerBase
    {
        private readonly ITaskPollingService _pollingService;

        public TaskPollingController(ITaskPollingService pollingService)
        {
            _pollingService = pollingService;
        }

        /// <summary>
        /// 启动轮询服务
        /// </summary>
        [HttpPost("start")]
        public IActionResult StartPolling()
        {
            _pollingService.Start();
            return Ok("轮询服务已启动");
        }

        /// <summary>
        /// 停止轮询服务
        /// </summary>
        [HttpPost("stop")]
        public IActionResult StopPolling()
        {
            _pollingService.Stop();
            return Ok("轮询服务已停止");
        }

        /// <summary>
        /// 获取所有任务的当前状态
        /// </summary>
        [HttpGet("tasks")]
        public IActionResult GetAllTasks()
        {
            var tasks = _pollingService.GetAllTasks();
            return Ok(tasks);
        }

        /// <summary>
        /// 获取单个任务的状态
        /// </summary>
        [HttpGet("tasks/{id}")]
        public IActionResult GetTask(int id)
        {
            var task = _pollingService.GetTaskById(id);
            if (task == null)
            {
                return NotFound();
            }
            return Ok(task);
        }

        /// <summary>
        /// 获取轮询服务的当前状态
        /// </summary>
        [HttpGet("status")]
        public IActionResult GetPollingStatus()
        {
            var status = _pollingService.GetPollingStatus();
            return Ok(status);
        }

    }
}